package logic

import (
	"context"
	"taobao-merchandise/models/mysql"

	"taobao-merchandise/userrpc/internal/svc"
	"taobao-merchandise/userrpc/userrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type GoodsDetailLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGoodsDetailLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GoodsDetailLogic {
	return &GoodsDetailLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 商品详情
func (l *GoodsDetailLogic) GoodsDetail(in *userrpc.GoodsDetailRequest) (*userrpc.GoodsDetailResponse, error) {
	// todo: add your logic here and delete this line

	var goods mysql.Goods
	err := goods.FindGoodsById(mysql.DB, in.GoodsId)
	if err != nil {
		return nil, err
	}

	return &userrpc.GoodsDetailResponse{
		GoodsId:   int64(goods.ID),
		GoodsName: goods.GoodsName,
		Price:     float32(goods.Price),
		Stock:     goods.Stock,
	}, nil
}
